/**
 * 
 */
package com.app.servlet.filter;

import java.io.IOException;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.context.support.XmlWebApplicationContext;

import com.app.commons.cache.ICacheService;
import com.app.service.vo.UserVo;

/**
 * @author yuxk
 * 
 */
public class CornFilter implements Filter {

	/**
	 * 
	 */
	public CornFilter() {
		// TODO Auto-generated constructor stub
	}

	private String encoder = "UTF-8";

	static private Logger logger = Logger.getLogger(CornFilter.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.Filter#destroy()
	 */
	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
	 * javax.servlet.ServletResponse, javax.servlet.FilterChain)
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain arg2) throws IOException, ServletException {
		// TODO Auto-generated method stub

		HttpServletRequest request = (HttpServletRequest) arg0;
		HttpServletResponse response = (HttpServletResponse) arg1;
		request.setCharacterEncoding(this.encoder);
		response.setCharacterEncoding(this.encoder);
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String path = request.getRequestURI();
		String contextpath = request.getContextPath();
		String key = path.substring(contextpath.length());
		ServletContext context = request.getSession().getServletContext();
		XmlWebApplicationContext webappcontex = (XmlWebApplicationContext) context.getAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.springMVC");
		ICacheService ics = (ICacheService) webappcontex.getBean("System-cache");		
		String[] admin = request.getSession().getServletContext().getInitParameter("admin").split(",");
		ics.saveObject("admin", admin[1]);
		if (key.equals("/login/user.form")) {
			
			if (null != username && null != password) {
				Cookie cookie = null;
				if (username.equals(admin[0]) && password.equals(admin[1])) {
					if (null != password && password.equals(admin[1])) {
						// 管理员登录
						cookie = new Cookie("logiccookie", username);
						cookie.setPath("/");
						cookie.setMaxAge(30 * 24 * 60 * 60);// 有效期30天
						response.addCookie(cookie);
						logger.info("管理员登录成功");
						request.getRequestDispatcher("/login/admin.form")
								.forward(request, response);
						return;
					}

				} else {

					
					Map<String, UserVo> usermap = (Map<String, UserVo>) ics
							.getObject("corn-uesr");
					UserVo vo = usermap.get(username);

					if (null != vo && vo.getPassword().equals(password)) {
						logger.info("普通登录成功");
						logger.info("获取用户权限");
						cookie = new Cookie("logiccookie", username);
						cookie.setPath("/");
						cookie.setMaxAge(30 * 24 * 60 * 60);// 有效期30天
						response.addCookie(cookie);
						request.getRequestDispatcher("/login/user.form")
								.forward(request, response);
						return;
					} else {
						request.getRequestDispatcher("/welcome.jsp").forward(
								request, response);
					}

				}
			}else{
				request.getRequestDispatcher("/login/user.form")
				.forward(request, response);
			}
		} else if(key.trim().equals("/ajax.form")){
			arg2.doFilter(request, response);
		}else{
			Cookie cookies[] = request.getCookies();
			for (Cookie c : cookies) {
				if (c.getName().equals("logiccookie")) {
					arg2.doFilter(request, response);
					return;
				}
			}
			response.sendRedirect(request.getContextPath() + "/welcome.jsp");

			return;

		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
	 */
	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		this.encoder = arg0.getInitParameter("encoder");

	}

}
